<?php

// Insert into SC_FORMULA (FORMULA_ID,SEQUENCE,TYPE,CODE,CLASS,DESCRIPTION,START_DATE,END_DATE,PCNT_TAX,LINE_ORDER)
//values (XX,1,'SHIPPING_COST','ShippingFixedPrice','CartWS_Model_FormulaEngine_Formula_ShippingFixedPrice','Costo de envio fijo $10',to_timestamp('16-SEP-09','DD-MON-RR HH.MI.SSXFF AM'),null,1,1000);

class CartWS_Model_FormulaEngine_Formula_ShippingPriceChileExpress extends CartWS_Model_FormulaEngine_Formula_Abstract {

    public function execute() 
    {
        // Formula Properties
        $rowSet = $this->getProperties();
        if( count( $rowSet )) {
            $i=0;
            $result = array();
            foreach( $rowSet as $row ) {
                $result[$i] = $row->toArray();
                $i++;
            }
        }
        $formulaProperties = $result;
        $result = array();
        foreach( $formulaProperties as $property )
        {
            $result[$property['property']] = $property['value'];
        }
        $formulaProperties = $result;

        // Informacion de la orden
        $row = $this->getOrder();
        $orderInfo = $row->toArray();
     
        if ( $orderInfo['shipping_method_id'] == null )
        {
            // No esta seteado el metodo de envio
            return CartWS_Model_FormulaEngine_Formula_Abstract::RET_CONTINUE;
        }

        if ( $orderInfo['shipping_method_id'] != $formulaProperties['shippingMethodId'] )
        {
            // No corresponde formula al metodo seleccionado
            return CartWS_Model_FormulaEngine_Formula_Abstract::RET_CONTINUE;
        } 
        

        if ( $orderInfo['ship_to_address_id'] == null || $orderInfo['ship_to_address_id'] == 0 )
        {
            // No esta seteado el ship_to_address_id
            return CartWS_Model_FormulaEngine_Formula_Abstract::RET_CONTINUE;
        }

        // Informacion para calculo
        $addressModel = $this->getDbTable( 'CartWS_Model_Address' );
        $zoneAddress = $addressModel->getZoneId( $orderInfo['ship_to_address_id'] );

        if ( $zoneAddress[0]['zone_id'] == null || $zoneAddress[0]['zone_id'] == 0 )
        {
            // No tiene zone_id asociado
            return CartWS_Model_FormulaEngine_Formula_Abstract::RET_CONTINUE;
        }

        $zoneId = $zoneAddress[0]['zone_id'];
        $shippingMethodId = $orderInfo['shipping_method_id'];

        // Informacion de las lineas
        $rowSet = $this->getOrderLines();
        $countItems = 0;
        if( count( $rowSet )) {
            $i=0;
            $result = array();
            foreach( $rowSet as $row ) {
                $result[$i] = $row->toArray();
                if ( $result[$i]['line_type'] == $formulaProperties['lineType'] )
                {
                    $countItems = $countItems + $result[$i]['ordered_quantity'];
                }
                $i++;
            }
        }
        $linesInfo = $result;

    	$zoneCostModel = $this->getDbTable( 'CartWS_Model_ZoneCost' );
        $costForMethod = $zoneCostModel->getCostForMethod( $zoneId, $shippingMethodId );
        $result = array();
        $i=0;
        foreach( $costForMethod as $row ) {
            $result[$i] = $row;
        }
        $totalShippingCost = $this->_calculeCostForShipping( $result[0]['default_cost'], $result[0]['additional_cost_item'],
                                                             $countItems, $formulaProperties );
        // Seteo el Shipping Cost
        $this->setMyAmount( $totalShippingCost );

        // No seguir calculando formulas
        return CartWS_Model_FormulaEngine_Formula_Abstract::RET_STOP;
    }

    private function _calculeCostForShipping( $defaultCost, $additionalCostItem, $countItems, $formulaProperties )
    {
error_log( $formulaProperties['limitDefault'] );
error_log( $countItems );
        $shippingCost = $defaultCost;
        if ( ($countItems - $formulaProperties['limitDefault'] ) > 0 )
        {
            $shippingCost = $shippingCost + ( $countItems - $formulaProperties['limitDefault'] ) * $additionalCostItem;
        } 
    
        return $shippingCost;
    }

}
